package com.ucar.train.service.impl;

import com.ucar.train.dao.*;
import com.ucar.train.dto.*;
import com.ucar.train.service.UserService;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wufuqiang
 * @title: UserServiceImpl
 * @projectName:train-parent
 * @description: T0D0
 * @created 2019-07-24 14:47
 */


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserGroupMapper userGroupMapper;

    @Autowired
    GroupMapper groupMapper;

    @Autowired
    RoleAndMenuMapper roleAndMenuMapper;

    @Autowired
    MenuMapper menuMapper;

    /**
     * 登入操作，判断用户的用户名密码
     * @param phone 这里手机号作为登入用户名
     * @return
     */
    @Override
    public User login(String phone) {
        return userMapper.getUserByPhone(phone);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public Integer register(User user) {
        //用户已存在，无法注册。（手机号或邮箱号已存在)
        if(userMapper.getUserByPhoneEmail(user.getPhone(),user.getEmail()) != null){
            return 0;//返回0
        }
        user.setCreateTime(JodaUtils.nowAsDate());
        user.setModifyTime(JodaUtils.nowAsDate());
        return userMapper.saveUser(user);
    }

    /**
     * 保存用户
     * @param user
     * @return
     */
    @Override
    public Integer saveUser(User user) {
        //用户已存在，无法注册。（手机号或邮箱号已存在)
        if(userMapper.getUserByPhoneEmail(user.getPhone(),user.getEmail()) != null){
            return 0;//返回0
        }
        user.setCreateTime(JodaUtils.nowAsDate());
        user.setModifyTime(JodaUtils.nowAsDate());
        user.setPassword("123456");
        return userMapper.saveUser(user);//保存用户信息
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @Override
    public Integer deleteUser(Long id) {
        userMapper.deleteUser(id);//删除用户信息
        userGroupMapper.deleteUserGroupByUserId(id);//删除该用户所有用户组关系
        return null;
    }

    /**
     * 根据id获取用户信息
     * @param id    用户Id
     * @return
     */
    @Override
    public User getUser(Long id) {
        return userMapper.getUser(id);//获取user信息
    }

    /**
     * 修改用户信息
     * @param user 用户对象
     * @return
     */
    @Override
    public Integer updateUser(User user) {
        userMapper.updateUser(user);
        return userMapper.updateUser(user);
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<User> listUsers() {
        List<User> users = userMapper.listUsers();
        return userMapper.listUsers();
    }

    /**
     * 根据组id查询该组用户信息
     * @param id
     * @return
     */
    @Override
    public List<User> listUserByGroupId(Long id) {
        return userMapper.listUsersByGroupId(id);
    }

    /**
     * 根据用户id查询用户角色id
     * @param id
     * @return
     */
    @Override
    public List<Long> listRolesByUser(long id) {
        return userRoleMapper.listRolesByUser(id);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public List<UserRole> listRoleByUser(Long id) {
        return userRoleMapper.listRoleByUser(id);
    }

    /**
     * 查询到该用户的所有权限信息
     * auth;吴福强
     * @param id
     * @return
     */
    @Override
    public Set<MenuDTO> listMenusByUser(Long id) {
        //查询该用户的所有角色ID
        List<Long> roleIds = userRoleMapper.listRolesByUser(id);
        //存储权限信息
        Set<MenuDTO> permissions = new HashSet<>();
        //查询该用户所有权限id
        for (Long roleId:roleIds) {
            //查询到该角色所有资源id
            List<Long> menuIds = roleAndMenuMapper.listMenusByRole(roleId);
            //根据资源id查询到该资源所有信息
            List<MenuDTO> menus = menuMapper.listMenusById(menuIds);
            permissions.addAll(menus);
        }
        return permissions;
    }

}
